/**
 * @projectDescription AsceptJS
 * @author xiaobinghan
 * @version 1.0
 */
(function(){
	AsceptJS = {
		max : 7,
		events : {
			before : 1,
			after : 2,
			around : 3
		},
		pickup : function(pointcut){
			var pickups = [];
			var paths = pointcut.split(".");
			if(paths.length > 7){
				throw "Pointcut execution too deep.";
			}
			var bases = [];
			var nextBases = [window];
			for(var i=0;i<paths.length;i++){				
				bases = nextBases;
				nextBases = [];
				var path = paths[i];
				for(var j=0;j<bases.length;j++){
					var base = bases[j];
					for(var p in base){
						if(p==path){
							if(i == paths.length-1 && typeof base[p] == "function"){
								pickups.push({"base":base,"funcName":p});
							}else if(i < paths.length-1){
								nextBases.push(base[p]);
							}
						}
					}
					
				}
			}
			return pickups;
		},
		register : function(pointcut,events,callback){
			var pickups = this.pickup(pointcut);
			for(var i=0;i<pickups.length;i++){
				var pickup = pickups[i];
				var base = pickup.base;
				var funcName = pickup.funcName;
				var func = base[funcName];
				var proxyFunc = null;
				switch(events){
					case this.events.before : 
						proxyFunc = function(){
							callback(base);
							func.apply(base,arguments);
						}
						break;
					
					case this.events.after : 
						proxyFunc = function(){
							func.apply(base,arguments);
							callback(base);
						}
						break;
					
					case this.events.around : 
						proxyFunc = function(){
							callback(func,arguments,base);
						}
						break;
										
				}
				if(proxyFunc!=null){
					base[funcName] = proxyFunc;
				}
			}
		}
	};
})()